﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using EC2011_hk1_BT8_1041074_1041130_1042119;

namespace Lab8_Security
{
    public partial class Member : System.Web.UI.Page
    {
        public string style = "display:none";
        protected void Page_Load(object sender, EventArgs e)
        {
            int IdRole = -1;
            User UserLogin = (User)Session["UserLogin"];
            if (UserLogin == null)
            {
                Message.InnerHtml = Server.HtmlEncode("You do not login, please logging to this page! ");
                return;
            }
            else
            {
                IdRole = UserLogin.IdRole;
                style = "";
                if (IdRole == 1)    // Admin
                {
                    Message.InnerHtml = Server.HtmlDecode("<b><i>You are Admin</i></b>");
                    UserName.Text = UserLogin.Username;
                    if(UserLogin.Birthday!=null)
                        Birthday.Text = UserLogin.Birthday.ToString();
                    if (UserLogin.Email != null)
                        Email.Text = UserLogin.Email;
                    if (UserLogin.Phone != null)
                        Phone.Text = UserLogin.Phone;
                }
                if(IdRole==2)   // Member
                {
                    Message.InnerHtml = Server.HtmlDecode("<b><i>You are Member</i></b>");
                    UserName.Text = UserLogin.Username;
                    if (UserLogin.Birthday != null)
                        Birthday.Text = UserLogin.Birthday.ToString();
                    if (UserLogin.Email != null)
                        Email.Text = UserLogin.Email;
                    if (UserLogin.Phone != null)
                        Phone.Text = UserLogin.Phone;
                }
            }
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            User userInfo = (User)Session["UserLogin"];

            if (userInfo.IdRole == 1) // Admin
            {
                Message.InnerHtml = Server.HtmlDecode("<b><i>You cannot change member info !</i></b>");
            }
            else
            {

                dbLab8DataContext dbLab8 = new dbLab8DataContext();
                User editData = dbLab8.Users.Single(m => m.Username == userInfo.Username);
                editData.Birthday = DateTime.Parse(Birthday.Text);
                editData.Email = Email.Text;
                editData.Phone = Phone.Text;

                dbLab8.SubmitChanges();

                Response.Redirect("Member.aspx");
            }
        }
    }
}